package com.fy.springmvc.controller;


import com.fy.springmvc.entity.User;
import com.fy.springmvc.vo.UserVo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/t1")
    public ModelAndView t1(){
        System.out.println("...");
        /**
         * Model： 模型 作用:封装数据
         * View:   视图 作用:展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("name", "张三");
        //设置视图名称
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @RequestMapping("/t2")
    public ModelAndView t2(ModelAndView modelAndView){
        System.out.println("...");
        modelAndView.addObject("name", "李四");
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @RequestMapping("/t3")
    public String t3(Model model){
        System.out.println("...");
        model.addAttribute("name", "王武");
        return "hello";
    }

    @RequestMapping("/t4")
    public String t4(HttpServletRequest request){
        /**
         * Model是springmvc给封装好的一个对象
         *request是javaWeb的tomcat产生的
         */
        request.setAttribute("name", "比目鱼" );
        return "hello";
    }

    @RequestMapping("/t5")
    /**
     * 回写数据 不进行视图跳转 返回值用void
     *  把"hello word" 返回到页面
     */
    public void t5(HttpServletResponse response) throws IOException {
     response.getWriter().println("hello word");
    }

    @RequestMapping("/t6")
    @ResponseBody//告知springmvc框架不要视图跳转 直接数据回写  不加此注解会进行url拼接=404
    public String t6(){
        return "hello 北极熊";
    }

    @RequestMapping("/t7")
    @ResponseBody//告知springmvc框架不要视图跳转 直接数据回写  不加此注解会进行url拼接=404
    public String t7(){
        return "{\"name\":\"zhangsan\", \"age\":18}";
    }

    @RequestMapping("/t8")
    @ResponseBody
    public User t8(){
        User user = new User();
        user.setName("lisi");
        user.setAge(15);
        //使用fastJson的转换工具 将对象转换为json格式的字符串再返回  在mvc.xml中进行配置
        //JSON.toJSONString(user);
        return user;
    }

    @RequestMapping("/t9")
    @ResponseBody //代表不进行页面跳转 回写数据
    public void t9(String name, int age){
        System.out.println(name);
        System.out.println(age);
    }

    @RequestMapping("/t10")
    @ResponseBody //代表不进行页面跳转 回写数据
    public void t10(User user){
        System.out.println(user.getName());
        System.out.println(user.getAge());
        System.out.println(user);
    }

    //获得数组请求参数
    @RequestMapping("/t11")
    @ResponseBody //代表不进行页面跳转 回写数据
    public void t11(String[] str){
        System.out.println(Arrays.toString(str));
    }

    //获得集合类型参数
    @RequestMapping("/t12")
    @ResponseBody
    public void t12(UserVo userVo){
        System.out.println(userVo);
    }

    @RequestMapping("/t13")
    @ResponseBody
    //形参列表中 使用@RequestBody 表示把请求体内容直接封装到集合当中
    public void t13(@RequestBody  List<User> userList){
        System.out.println(userList);
    }

    @RequestMapping("/t14")
    @ResponseBody
    public void t14(@RequestParam("name") String username){
        System.out.println(username );
    }

    /**
     *   localhost:8080/user/t15/张三
     *   @PathVariable主要用在Restful风格的请求，不是以键值对形式，
     *   是直接把参数放在url地址上并配合四种请求方式（get、post、put、delete），
     *   通过@PathVariable去解析url地址上的参数
     */
    @RequestMapping("/t15/{name}")
    @ResponseBody
    public void t15(@PathVariable("name") String name){
        System.out.println(name );
    }

    @RequestMapping("/t16")
    public void t15(Date date){
        System.out.println(date);
    }

    @RequestMapping("/t17")
    public void t17(HttpServletRequest request , HttpServletResponse response, HttpSession session){
        System.out.println(request);//org.apache.catalina.connector.RequestFacade@500824a3
        System.out.println(response);//org.apache.catalina.connector.ResponseFacade@23552bcf
        System.out.println(session);//org.apache.catalina.session.StandardSessionFacade@5a017146
    }

    //获取请求头数据
    @RequestMapping("/t18")
    public void t18(@RequestHeader(value = "User-Agent", required = false) String user_agent){
        System.out.println(user_agent);
    }

    //获取请求头 Cookie的值
    @RequestMapping("/t19")
    public void t19(@CookieValue(value = "JSESSIONID") String jsessionId){
        System.out.println(jsessionId);
    }

    //单文件上传
    @RequestMapping("/t20")
    public void t20(String name, MultipartFile uploadFile) throws IOException {
        System.out.println(name);
        // 获得上传文件的名称
        String filename = uploadFile.getOriginalFilename();
        //上传的文件 transferTo转移到==>指定磁盘路径
        uploadFile.transferTo(new File("/Users/flying/Desktop/upload/"+filename));
    }

    //多文件上传
    @RequestMapping("/t21")
    public void t21(String name, MultipartFile[] uploadFile) throws IOException {
        System.out.println(name);
        for (MultipartFile multipartFile : uploadFile ) {
            String filename = multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File("/Users/flying/Desktop/upload/"+ filename));
        }
    }
}
